package com.example.home.repo

import com.example.common.api.BaseApiService
import com.example.common.entity.CommendEntity
import com.example.common.entity.SimpleVideoEntity
import com.example.common.repo.BaseRepository
import com.example.home.api.HomeApiServer
import com.example.home.entity.SimpleTypeEntity
import com.example.http.OkHttpClientManager
import com.example.http.RetrofitManager
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import okhttp3.RequestBody
import javax.inject.Inject

class HomeRepository:BaseRepository<HomeApiServer>(){

    override fun createApiServer(): HomeApiServer {
        return RetrofitManager.getRetrofit(OkHttpClientManager.createOkHttpClient()).create(HomeApiServer::class.java)
    }

    suspend fun getSimpleType():Flow<List<SimpleTypeEntity>>{
        return flow {
            var result=apiServer.getSimpleType()
            if(result.code==200){
                emit(result.data)
            }else{
                emit(listOf())
            }
        }
    }


    suspend fun getRecommendSimpleVideoList(pageIndex:Int,pageSize: Int):Flow<List<SimpleVideoEntity>>{
        return flow {
            var result=apiServer.getRecommendSimpleVideo(pageIndex,pageSize)
            if(result.code==200){
                emit(result.data)
            }else{
                emit(listOf())
            }
        }
    }
    suspend fun getSimpleVideoList(channelId: String,pageIndex:Int,pageSize: Int):Flow<List<SimpleVideoEntity>>{
        return flow {
            var result=apiServer.getSimpleVideoByChannelId(channelId,pageIndex,pageSize)
            if(result.code==200){
                emit(result.data)
            }else{
                emit(listOf())
            }
        }
    }
    suspend fun getVideoCommends(dataType:Int,itemId: String):Flow<List<CommendEntity>>{
        return flow {
            var result=apiServer.getVideoCommends(dataType,itemId)
            if(result.code==200){
                emit(result.data)
            }else{
                emit(listOf())
            }
        }
    }
    suspend fun publishVideoComment(requestBody:RequestBody):Flow<CommendEntity>{
        return flow {
            var result=apiServer.publishVideoComment(requestBody)
            if(result.code==200){
                emit(result.data)
            }
        }
    }


}
